package com.wqr.mobile.android.ui.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;

import com.wqr.mobile.android.ui.R;

/**
 * 
 * @author wenqiurong
 * @param <T>
 * @param <E>
 * 
 */
public abstract class MBaseListAdapter<T, E> extends ArrayAdapter<T> implements OnItemClickListener {
	public String TAG;
	private Context mContext;
	private int mLayoutId;
	private List<T> mItems;
	private boolean mScrollEable;
	private E mHolder;

	public MBaseListAdapter(Context context, int itemLayoutId, E holder, List<T> items) {
		super(context, itemLayoutId, items);
		this.mContext = context;
		this.mLayoutId = itemLayoutId;
		this.mHolder = holder;
		this.mItems = items;
		TAG = getClass().getSimpleName();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (mScrollEable && position == mItems.size()) {
			return loadItems();
		}
		E holder;
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(mLayoutId, null);
			holder = mHolder;
			findHolderViews(convertView, holder);
			convertView.setTag(holder);
		} else {
			holder = (E) convertView.getTag();
		}
		setItemFromHolder(holder, getItem(position));
		return convertView;
	}

	/**
	 * find Holder Views
	 * 
	 * @param convertView
	 * @param holder
	 */
	public abstract void findHolderViews(View convertView, E holder);

	/**
	 * set Item From Holder
	 * 
	 * @param holder
	 * @param item
	 */
	public abstract void setItemFromHolder(E holder, T item);

	/**
	 * on Item Click
	 * 
	 * @param parent
	 * @param view
	 * @param position
	 * @param id
	 */
	public abstract void _onItemClick(Context context, AdapterView<?> parent, View view, int position, long id, T t);

	/**
	 * set OnItemClickListener
	 * 
	 * @param view
	 */
	public void setOnItemClickListener(AbsListView view) {
		view.setOnItemClickListener(this);
	}

	/**
	 * add list after loading
	 * 
	 * @param anotherCommentList
	 */
	public void addList(List<T> items) {
		if (items == null) {
			mItems = new ArrayList<T>();
		}
		mItems.addAll(items);
		this.notifyDataSetChanged();
	}

	public void setScrollEable(boolean scrollEable) {
		this.mScrollEable = scrollEable;
	}

	@Override
	public T getItem(int position) {
		return mItems.get(position);
	}

	@Override
	public int getCount() {

		if (mItems == null) {
			return 0;
		} else if (mScrollEable) {
			return mItems.size() + 1;
		} else {
			return mItems.size();
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		_onItemClick(mContext, parent, view, position, id, getItem(position));
	}

	/**
	 * loading
	 * 
	 * @return
	 */
	private View loadItems() {
		LinearLayout layout = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.layout_load, null);
		return layout;
	}
}
